package com.eco.netty;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eco.controller.LightController;
import com.eco.utils.ApplicationContextProvider;
import com.eco.utils.NettyCtxMap;
import com.github.benmanes.caffeine.cache.Cache;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/Sensor3HexHandler.class */
public class Sensor3HexHandler extends ChannelInboundHandlerAdapter {
    private NettyClient nettyClient;
    private Cache<String, Object> cklsCache;
    private StringRedisTemplate stringRedisTemplate;
    private LightController lightController;
    private Long startTime;
    private static final Logger log = LoggerFactory.getLogger((Class<?>) Sensor3HexHandler.class);
    private static JSONObject result = new JSONObject();

    public Sensor3HexHandler() {
        new ApplicationContextProvider();
        this.nettyClient = (NettyClient) ApplicationContextProvider.getBean(NettyClient.class);
        new ApplicationContextProvider();
        this.cklsCache = (Cache) ApplicationContextProvider.getBean(Cache.class);
        new ApplicationContextProvider();
        this.stringRedisTemplate = (StringRedisTemplate) ApplicationContextProvider.getBean(StringRedisTemplate.class);
        new ApplicationContextProvider();
        this.lightController = (LightController) ApplicationContextProvider.getBean(LightController.class);
        this.startTime = null;
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE) {
                ctx.channel().close();
                return;
            }
            return;
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("出口流速tcp有新连接");
        NettyCtxMap.add("sensor3", ctx);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String host = this.nettyClient.getSensor3Host();
        int port = this.nettyClient.getSensor3Port();
        log.error("与出口流速tcp服务" + host + ":" + port + "连接断开!");
        ctx.close();
        ctx.deregister();
        ctx.pipeline().remove(this);
        this.nettyClient.sensor3Reconnect(host, Integer.valueOf(port));
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        try {
            this.cklsCache.put("ckls", msg);
            if (this.stringRedisTemplate.opsForValue().getBit("autoStatus", 1L).booleanValue()) {
                JSONArray ckls = (JSONArray) msg;
                if (ckls == null || StringUtils.isEmpty(ckls.getString(0))) {
                    return;
                }
                String nowWaterLevel = new BigDecimal(ckls.getString(0)).add(new BigDecimal("0.07")).setScale(2, 1).toString();
                String waterLevel = this.stringRedisTemplate.opsForValue().get("waterLevel");
                if (StringUtils.isNotEmpty(nowWaterLevel) && StringUtils.isNotEmpty(waterLevel)) {
                    if (Float.valueOf(nowWaterLevel).floatValue() < Float.valueOf(waterLevel).floatValue()) {
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 1L).booleanValue()) {
                            this.lightController.order2Knd("in", "off1");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 2L).booleanValue()) {
                            this.lightController.order2Knd("in", "off2");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 3L).booleanValue()) {
                            this.lightController.order2Knd("in", "off3");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 4L).booleanValue()) {
                            this.lightController.order2Knd("in", "off4");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 5L).booleanValue()) {
                            this.lightController.order2Knd("in", "off5");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 6L).booleanValue()) {
                            this.lightController.order2Knd("in", "off6");
                        }
                        if (this.stringRedisTemplate.opsForValue().getBit("in", 7L).booleanValue()) {
                            this.lightController.order2Knd("in", "off7");
                        }
                    } else {
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 1L).booleanValue()) {
                            this.lightController.order2Knd("in", "on1");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 2L).booleanValue()) {
                            this.lightController.order2Knd("in", "on2");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 3L).booleanValue()) {
                            this.lightController.order2Knd("in", "on3");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 4L).booleanValue()) {
                            this.lightController.order2Knd("in", "on4");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 5L).booleanValue()) {
                            this.lightController.order2Knd("in", "on5");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 6L).booleanValue()) {
                            this.lightController.order2Knd("in", "on6");
                        }
                        if (!this.stringRedisTemplate.opsForValue().getBit("in", 7L).booleanValue()) {
                            this.lightController.order2Knd("in", "on7");
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("与出口流速tcp服务连接异常!");
        ctx.close();
    }
}